var api = require('../../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navs: [
      '可使用', '不可用'
    ],
    curindex: 0,
    lists: [],
    page: 1,
    lastPage: 0,
    type: 0,
    isUse: 0,
    service_id: '',
    longitude: '',
    latitude: '',
    car_num:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.isUse) {
      this.setData({
        isUse: options.isUse,
        service_id: options.service_id,
        longitude: options.longitude,
        latitude: options.latitude,
        carId:options.car_id,
        price:options.price,
        car_num:options.car_num,
        shop_id:options.shop_id
      })
    }
  },
  toDetial(e){
    let index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '/pages/card_details/card_details?isUser=1&id='+this.data.lists[index].id,
    })
  },
  toBuyCar() {
    wx.navigateTo({
      url: '/pages/buy_number_card/buy_number_Card?shop_id='+this.data.shop_id
    })
  },
  //选择优惠券
  selectedCard(e) {
    if(!this.data.isUse){ return false;}
    const index = e.currentTarget.dataset.index;
    this.setData({
      curindex: index
    })
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    var prevPage = pages[pages.length - 2];
    var listsItem = this.data.lists[index];

    var discount = 0;
    listsItem.services.map(item=>{
      console.log(item)
      console.log(item.service_id , this.data.service_id , item.car_num , this.data.car_num)
        if(item.service_id == 0){ //通用卡
          if(item.car_num == this.data.car_num){
            if(item.number == 0){  //如果没有次数，就给个打折
              console.log('没次数')
              discount = item.discount
            }
          }
        }else{
          if(item.service_id == this.data.service_id && item.car_num == this.data.car_num){
            if(item.number == 0){  //如果没有次数，就给个打折
              console.log('没次数')
              discount = item.discount
            }
          }
        }

    })
    console.log('discount',discount)
    let price = this.data.price;
    if(discount > 0){  //换算出优惠价
      if(discount == 10){
        price = '-1';
      }else{
        price = (price * (discount /10)).toFixed(2)
      }
    }else{
      price = 0
    }
    listsItem.servicePrice = price
    console.log('金额',price)
    prevPage.setData({
      selectUserCard: listsItem,
      pay_way:2
    })
    wx.navigateBack({
      delta: 1,
    })
  },
  getCard() {

    if (this.data.lastPage != 0 && this.data.page >= this.data.lastPage) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
      return false;
    }
    api.get('/card/list', {
      type: this.data.type,
      page: this.data.page
    }).then(res => {
      this.setData({
        lists: res.items,
        lastPage: res.lastPage,
        page: this.data.page + 1
      })
    })
  },
  getCardAllows() {

    if (this.data.lastPage != 0 && this.data.page >= this.data.lastPage) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
      return false;
    }
    api.get('/card/allows', {
      service_id: this.data.service_id,
      longitude: this.data.longitude,
      latitude: this.data.latitude,
      car_id:this.data.carId
    }).then(res => {
      this.setData({
        lists: res.items,
        lastPage: res.lastPage,
        page: this.data.page + 1
      })
    })
  },
  //点击导航
  click_nav(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      curindex: index,
      type: index,
      page: 1,
      lastPage: 0,
      lists: []
    })
    if (this.data.isUse) {
      this.getCardAllows();
    } else {
      this.getCard();
    }


  },
  click_receive_more_coupon() {
    wx.navigateTo({
      url: '../coupons/coupons',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.isUse) {
      this.getCardAllows();
    } else {
      this.getCard();
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getCoupon();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})